package com.example.dynamicdatabase.service;

import com.example.dynamicdatabase.databasemodel.DataBaseInfo;
import com.example.dynamicdatabase.mapper.DataBaseInfoMapper;
import com.example.dynamicdatabase.mapper.DynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description TODO
 * @Author 奶茶
 * Created by on 2018/11/25 15:47.
 */
@Service
public class DataBaseInfoService {

    @Autowired
    private DataBaseInfoMapper dataBaseInfoMapper;
    @Autowired
    private DynamicMapper dynamicMapper;

    private static ConcurrentHashMap<String,DataBaseInfo> baseDataSourceMap = new ConcurrentHashMap<>();

    public DataBaseInfo getDataBaseInfoByAppKey(String appKey){
        DataBaseInfo dataBaseInfo;
        if(!baseDataSourceMap.containsKey(appKey)){
            dataBaseInfo = dataBaseInfoMapper.getDataBaseInfoByAppKey(appKey);
            baseDataSourceMap.put(appKey,dataBaseInfo);
        }else{
            dataBaseInfo = baseDataSourceMap.get(appKey);
        }
        return dataBaseInfo;
    }

    public List<Map<String,Object>> getDynamicData(){
        return dynamicMapper.findAll();
    }

    public List<Map<String,Object>> getDynamicData2(){
        return dynamicMapper.findAll2();
    }


}
